<template>
  <Form ref='regRef' class="xtx-form" v-slot='{errors}'>
    <div class="xtx-form-item">
      <div class="field">
        <i class="icon iconfont icon-user"></i>
        <Field v-model="form.account" name='account' :rules='schema.accountApi' class="input" type="text" placeholder="请输入用户名" />
      </div>
      <div class="error" v-if='errors.account'>{{errors.account}}</div>
    </div>
    <div class="xtx-form-item">
      <div class="field">
        <i class="icon iconfont icon-phone"></i>
        <Field v-model="form.mobile" name='mobile' :rules='schema.mobile' class="input" type="text" placeholder="请输入手机号" />
      </div>
      <div class="error" v-if='errors.mobile'>{{errors.mobile}}</div>
    </div>
    <div class="xtx-form-item">
      <div class="field">
        <i class="icon iconfont icon-code"></i>
        <Field v-model="form.code" name='code' :rules='schema.code' class="input" type="text" placeholder="请输入验证码" />
        <span class="code" @click='sendCode'>{{time === 0 ? '发送验证码': `${time}秒后重发`}}</span>
      </div>
      <div class="error" v-if='errors.code'>{{errors.code}}</div>
    </div>
    <div class="xtx-form-item">
      <div class="field">
        <i class="icon iconfont icon-lock"></i>
        <Field v-model="form.password" name='password' :rules='schema.password' class="input" type="password" placeholder="请输入密码" />
      </div>
      <div class="error" v-if='errors.password'>{{errors.password}}</div>
    </div>
    <div class="xtx-form-item">
      <div class="field">
        <i class="icon iconfont icon-lock"></i>
        <Field name='repassword' :rules='schema.repassword' class="input" type="password" placeholder="请确认密码" />
      </div>
      <div class="error" v-if='errors.repassword'>{{errors.repassword}}</div>
    </div>
    <a href="javascript:;" class="submit" @click='handleReg'>立即提交</a>
  </Form>
</template>

<script>
import { Form, Field } from 'vee-validate'
import schema from '@/utils/vee-validate-schema'
import { userQQPatchCode } from '@/api/user.js'
import { getCurrentInstance, reactive, ref } from 'vue'
import { useIntervalFn } from '@vueuse/core'
import { useStore } from 'vuex'
import { useRouter } from 'vue-router'

export default {
  name: 'CallbackPatch',
  components: { Form, Field },
  props: {
    unionId: {
      type: String,
      required: true
    }
  },
  setup (props) {
    const store = useStore()
    const router = useRouter()
    const instance = getCurrentInstance()
    // 表单数据
    const form = reactive({
      account: '',
      mobile: '',
      code: '',
      password: ''
    })

    // 控制倒计时
    const time = ref(0)
    // pause 实现暂停
    // resume 实现重启：点击发送开始启动
    const { pause, resume } = useIntervalFn(() => {
      if (time.value > 0) {
        time.value--
      } else {
        // 停止倒计时
        pause()
      }
    }, 1000)

    // 发送手机验证码
    const sendCode = async () => {
      try {
        // 启动倒计时
        time.value = 60
        resume()

        const ret = await userQQPatchCode(form.mobile)
        if (ret.msg === '操作成功') {
          // 发送验证码成功
          instance.proxy.$message({
            type: 'success',
            text: '发送验证码成功'
          })
        }
      } catch {
        instance.proxy.$message({
          type: 'error',
          text: '发送验证码失败'
        })
      }
    }

    // 注册账号提交表单
    const regRef = ref(null)
    const handleReg = async () => {
      const valid = await regRef.value.validate()
      if (!valid) return
      // 表单验证通过，调用接口
      const ret = await store.dispatch('user/registerAccount', {
        unionId: props.unionId,
        form: form
      })
      if (ret) {
        // 注册成功
        instance.proxy.$message({
          type: 'success',
          text: '注册成功'
        })
        // 跳转到首页面
        router.push('/')
      } else {
        // 注册失败
        instance.proxy.$message({
          type: 'error',
          text: '注册失败'
        })
      }
    }

    return { schema, sendCode, form, time, handleReg, regRef }
  }
}
</script>

<style scoped lang='less'>
.code {
  position: absolute;
  right: 0;
  top: 0;
  line-height: 50px;
  width: 80px;
  color: #999;
  &:hover {
    cursor: pointer;
  }
}
</style>
